package com.jivam.lottery.model;

import java.io.Serializable;

/**
 * 七星彩中的期数
 * @author Jivam
 */
public class PeriodNum implements Serializable{
	
	private static final long serialVersionUID = 7533202795911184964L;
	private int id;//期数
	/**
	 * 期数数值，对应位置看 Constants.POSTION_*
	 * sum4 前4位的和，位置对应7
	 * span4 跨度值(最大值-最小值)，位置对应8
	 */
	private int[] period = new int[9];
	
	public PeriodNum() {
	}

	public PeriodNum(int periodNum,int pid) {
		id = pid;
		int max = -1,min = 10;
		for(int i=6 ; i >-1 ; i--){
			period[i] = periodNum % 10;
			if(i<4){
				max = Math.max(max, period[i]);
				min = Math.min(min, period[i]);
			}
			periodNum /= 10;
		}
		period[8] = max - min;//跨度值
		init();
	}
	
	/**
	 * 其他数值处理
	 */
	private void init(){
		period[7] = period[0]+period[1]+period[2]+period[3];
	}
	
	/**
	 * 获得对应位置的值
	 * @param i
	 * @return
	 */
	public int getBit(int i) {
		if(i < 0 || i >= period.length) return 0;
		return period[i];
	}
	
	/**
	 * sum of positions
	 * @param ps
	 * @return
	 */
	public int getBitSum(int... ps) {
		int sum = 0;
		for(int p : ps){
			sum += period[p];
		}
		return sum%10;
	}
	
	public int getKeyResult(int[] ps) {
		return getBitSum(ps);
	}
	
	public int[] getPeriod() {
		return period;
	}

	public int getSum4() {
		return period[7];
	}
	
	public int getSpan4() {
		return period[8];
	}

	public int getId() {
		return id;
	}
	
	public String periodToString() {
		StringBuilder s = new StringBuilder();
		s.append(id);
		s.append(",");
		s.append(toString());
		return s.toString();
	}

	@Override
	public String toString() {
		StringBuilder s = new StringBuilder();
		for(int i = 0 ; i < 7;i++){
			s.append(period[i]);
		}
		return s.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PeriodNum other = (PeriodNum) obj;
		if (id != other.id)
			return false;
		return true;
	}
	
}
